import multiprocessing
import sys
import time

import queue

from PyQt5 import QtWidgets

from UI import Ui_MainWindow
from Emit_Image import ThreadEmitImage


class MyMainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(MyMainWindow, self).__init__()
        self.setupUi(self)
        self.initUI()

        self.my_thread_queue_image_done = queue.PriorityQueue()
        self.my_thread_queue_image_read = queue.PriorityQueue()
        self.my_process_queue_image_processing = multiprocessing.Queue()
        self.my_process_queue_image_processed = multiprocessing.Queue()
        self.my_end = multiprocessing.Queue()

        self.my_thread = ThreadEmitImage(self.my_thread_queue_image_done, self.my_thread_queue_image_read,
                                         self.my_process_queue_image_processing, self.my_process_queue_image_processed,
                                         self.my_end)
        self.my_thread.my_signal_image.connect(self.show_image)

        self.pushButton.clicked.connect(self.my_start)

        self.last_time = time.time()

    def initUI(self):
        self.setWindowTitle("Video")
        self.setFixedSize(self.width(), self.height())

    def show_image(self, image):
        this_time = time.time()
        last_time = self.last_time
        self.last_time = this_time
        try:
            self.label_fps.setText(str(1 / (this_time - last_time)))
            # print(str(1 / (this_time - last_time)))
        except:
            pass
        self.label.setPixmap(image[0])

    def my_start(self):
        self.my_thread.start()


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    MyUI = MyMainWindow()
    MyUI.show()
    sys.exit(app.exec_())
